﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hired
{
    public partial class results : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["fromAdvancedSearch"] == "true")
            {
                advancedSearch();
            }
            else {
                regularSearch(Request.QueryString["search"]);
            }
        }

        public void regularSearch(string searchQuery)
        {
            if (searchQuery != null && searchQuery != "")
            {
                var jobsList = DBJob.searchJob(searchQuery);
                //var coursesList = DBCourse.searchCourse(searchQuery);
                List<JobseekerAccount> peopleResult = DBJobseekerAccount.searchJobseekerAccount(searchQuery);
                var peopleList = returnPeopleList(peopleResult);                
                List<Company> companiesResult = DBCompany.searchCompany(searchQuery);
                var companiesList = returnCompaniesList(companiesResult);

                //var resourcesResult = DBJob.searchJob(searchQuery);            
                //var questionsResult = DBQuestion.searchQuestion(searchQuery);
                //var answersResult = DBAnswer.searchAnswer(searchQuery);

                //Jobs
                if (jobsList.Count != 0)
                {
                    gridViewResultsJobs.DataSource = jobsList;
                    gridViewResultsJobs.DataBind();
                }
                else
                {
                    lblJobsResults.Visible = true;
                }
                //People
                if (peopleList.Count != 0)
                {
                    gridViewResultsPeople.DataSource = peopleList;
                    gridViewResultsPeople.DataBind();
                }
                else
                {
                    lblPeopleResults.Visible = true;
                }
                ////Courses
                //if (coursesList.Count != 0)
                //{
                //    gridViewResultsCourses.DataSource = coursesList;
                //    gridViewResultsCourses.DataBind();
                //}
                //else
                //{
                //    //lblCoursesResults.Visible = true;
                //}
                //Companies
                if (companiesResult.Count != 0)
                {
                    gridViewResultsCompanies.DataSource = companiesList;
                    gridViewResultsCompanies.DataBind();
                }
                else
                {
                    lblCompaniesResults.Visible = true;
                }
                //Resources
                //    if (jobsResult.Count != 0)
                //    {
                //        gridViewResultsJobs.DataSource = Session["jobsResult"];
                //    }
                //    else
                //    {
                //        lblJobsResults.Visible = true;
                //    }
                //}
                ////Questions
                //if (questionsResult.Count != 0)
                //{
                //    gridViewResultsQuestions.DataSource = questionsResult;
                //    gridViewResultsQuestions.DataBind();
                //}
                //else
                //{
                //    lblQuestionsResults.Visible = true;
                //}
                ////Answers
                //if (answersResult.Count != 0)
                //{
                //    gridViewResultsAnswers.DataSource = answersResult;
                //    gridViewResultsAnswers.DataBind();
                //}
                //else
                //{
                //    lblAnswersResults.Visible = true;
                //}
            }
        }       
        public void advancedSearch() {
             string category = Request.QueryString["category"];
                string keywords = Request.QueryString["keywords"];
                string exclude = Request.QueryString["exclude"];
                if (keywords != null && keywords != "")
                {
                    #region Jobs
                    if (category == "Jobs")
                    {
                        //pnlAnswersResults.Visible = false;
                        pnlCompaniesResults.Visible = false;
                        TitlePanelCompanies.Visible = false;
                        //pnlCoursesResults.Visible = false;
                        //TitlePanelCourses.Visible = false;
                        pnlPeopleResults.Visible = false;
                        TitlePanelPeople.Visible = false;
                        //pnlQuestionsResults.Visible = false;
                        //pnlResourcesResults.Visible = false;
                        //TitlePanelResources.Visible = false;

                        string jobLocation = Request.QueryString["jobLocation"];
                        string jobIndustry = Request.QueryString["jobIndustry"];
                        string salaryUpperLimit = Request.QueryString["salaryUpperLimit"];
                        string salaryLowerLimit = Request.QueryString["salaryLowerLimit"];
                        int salaryMin = 0;
                        int salaryMax = 0;
                        if (salaryLowerLimit != "")
                        {
                            salaryMin = Convert.ToInt32(salaryLowerLimit);
                        }
                        if (salaryUpperLimit != "")
                        {
                            salaryMax = Convert.ToInt32(salaryUpperLimit);
                        }

                        List<Job> results = DBJob.advancedSearchJob(DBJob.searchJob(keywords), exclude, jobLocation, jobIndustry, salaryMin, salaryMax);
                        if (results.Count != 0)
                        {
                            gridViewResultsJobs.DataSource = results;
                            gridViewResultsJobs.DataBind();
                        }
                        else
                        {
                            lblJobsResults.Visible = true;
                        }
                    }
                    #endregion
                    #region People
                    if (category == "People")
                    {
                       // pnlAnswersResults.Visible = false;
                        pnlCompaniesResults.Visible = false;
                        TitlePanelCompanies.Visible = false;
                        //pnlCoursesResults.Visible = false;
                        //TitlePanelCourses.Visible = false;
                        pnlJobsResults.Visible = false;
                        TitlePanelJobs.Visible = false;
                       // pnlQuestionsResults.Visible = false;
                        //pnlResourcesResults.Visible = false;
                        //TitlePanelResources.Visible = false;

                        string educationInstitute = Request.QueryString["educationalInstitute"];
                        List<JobseekerAccount> results = DBJobseekerAccount.advancedSearchJobseekerAccount(DBJobseekerAccount.searchJobseekerAccount(keywords), educationInstitute, exclude);
                        var peopleList = returnPeopleList(results);
                        if (peopleList.Count != 0)
                        {
                            gridViewResultsPeople.DataSource = results;
                            gridViewResultsPeople.DataBind();
                        }
                        else
                        {
                            lblPeopleResults.Visible = true;
                        }
                    }
                    #endregion
                    #region Companies
                    if (category == "Companies")
                    {
                        //pnlAnswersResults.Visible = false;
                        pnlPeopleResults.Visible = false;
                        TitlePanelPeople.Visible = false;
                        //pnlCoursesResults.Visible = false;
                        //TitlePanelCourses.Visible = false;
                        pnlJobsResults.Visible = false;
                        TitlePanelJobs.Visible = false;
                        //pnlQuestionsResults.Visible = false;
                        //pnlResourcesResults.Visible = false;
                        //TitlePanelResources.Visible = false;

                        string email = Request.QueryString["companyEmail"];
                        List<Company> results = DBCompany.advancedSearchCompany(DBCompany.searchCompany(keywords), email, exclude);
                        var companiesList = returnCompaniesList(results);
                        if (companiesList.Count != 0)
                        {
                            gridViewResultsCompanies.DataSource = results;
                            gridViewResultsCompanies.DataBind();
                        }
                        else
                        {
                            lblCompaniesResults.Visible = true;
                        }
                    }

                    #endregion
                    //#region Courses
                    //if (category == "Courses")
                    //{
                    //    //pnlAnswersResults.Visible = false;
                    //    pnlPeopleResults.Visible = false;
                    //    TitlePanelPeople.Visible = false;
                    //    pnlCompaniesResults.Visible = false;
                    //    TitlePanelCompanies.Visible = false;
                    //    pnlJobsResults.Visible = false;
                    //    TitlePanelJobs.Visible = false;
                    //   // pnlQuestionsResults.Visible = false;
                        
                    //    string courseIndustry = Request.QueryString["relevantIndustry"];
                    //    string courseLocation = Request.QueryString["courseLocation"];

                    //    List<Course> results = DBCourse.advancedSearchCourse(DBCourse.searchCourse(keywords), courseIndustry, courseLocation, exclude);
                    //    if (results.Count != 0)
                    //    {
                    //        gridViewResultsCourses.DataSource = results;
                    //        gridViewResultsCourses.DataBind();
                    //    }
                    //    else
                    //    {
                    //        lblCoursesResults.Visible = true;
                    //    }

                    //}
                    //#endregion
                    if (category == "")
                    {
                        regularSearch(keywords);
                    }
                }
            }
        
        public List<object> returnPeopleList(List<JobseekerAccount> peopleResult){
            return
                    (from p in peopleResult
                     select new
                     {
                         Email = p.Email,
                         Username = p.Username,
                         FirstName = p.Jobseeker.FirstName,
                         LastName = p.Jobseeker.LastName,
                         Specialization = p.Jobseeker.Specialization,
                         WebsiteURL = p.Jobseeker.WebsiteURL
                     }).ToList<object>();
        }
        public List<object> returnCompaniesList(List<Company> companiesResult) {
            return
                (from c in companiesResult
                 select new {
                     Address = c.Address,
                     PostalCode = c.PostalCode,
                     Username = c.CompanyAccount.Username,
                     CompanyName = c.CompanyName,
                     ContactNumber = c.ContactNumber,
                     WebsiteURL = c.WebsiteURL,
                     Email = c.CompanyAccount.Email
                 }).ToList<object>();
        }
       
   

}
        
    }



